package com.kongxiang.cubebit.llm.config;

import org.springframework.ai.autoconfigure.openai.OpenAiChatProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestClient;
import org.springframework.web.reactive.function.client.WebClient;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.function.Consumer;

@Configuration
public class WebClientReactorConfig {

    @Autowired
    OpenAiChatProperties openAiChatProperties;

    @Bean
    public WebClient webClient() {
        return WebClient.builder()
                // 配置
                .baseUrl(openAiChatProperties.getBaseUrl())
                .defaultHeaders(new Consumer<HttpHeaders>() {
                    @Override
                    public void accept(HttpHeaders httpHeaders) {
                        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
                        httpHeaders.setAcceptCharset(Collections.singletonList(StandardCharsets.UTF_8));
                    }
                })
                .build();
    }

    @Bean
    public RestClient restClient() {
        return RestClient.builder().baseUrl(openAiChatProperties.getBaseUrl())
                 .defaultHeaders(new Consumer<>() {
                     @Override
                     public void accept(HttpHeaders httpHeaders) {
                         httpHeaders.setContentType(MediaType.APPLICATION_JSON);
                         httpHeaders.setAcceptCharset(Collections.singletonList(StandardCharsets.UTF_8));
                     }
                 }).build();
    }
}